
//create user's image view

Ext.define('CDR.widget.UserView', {
  extend: 'Ext.view.View',
  alias : 'widget.userview',
  tpl: [
      '<tpl for=".">',
          '<div class="thumb-wrap">',
              '<div class="thumb">',
                  (!Ext.isIE6? '<img src="resources/app/images/{thumb}" />' : 
                  '<div style="width:76px;height:76px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'resources/app/images/{thumb}\')"></div>'),
              '</div>',
              '<span>{name}</span>',
          '</div>',
      '</tpl>'
  ],
  
  itemSelector: 'div.thumb-wrap',
  multiSelect: true,
  singleSelect: false,
  cls: 'x-image-view',
  autoScroll: true,
  adminUser: null,
  treeView: null,
  initComponent: function() {
      this.store = Ext.create('Ext.data.Store', {
          fields: ['name', 'thumb', 'obId', 'type', 'admin']
      });
      this.menu = Ext.createWidget('menu',{
  		items:[{
  				text: CDR.I18N['organization.context-menu.set-admin'],
  				iconCls: 'icon-add',
  				handler: function() {
  					var selectNode = this.getSelectionModel().getLastSelected();
  					var siteNode = this.treeView.getSelectedNode();
  					var adminId;
  					if(this.adminUser != null){
  						adminId = this.adminUser.data.obId;
  					}
  					Ext.Ajax.request({
							scope: this,
	  						method: 'POST',
	  						url:'set-as-admin',
	  						params: {'siteId':siteNode.data.id, 'oldUserId': adminId, 'userId': selectNode.data.obId},
	  						success: function(response, opts){
	  							var obj = Ext.decode(response.responseText);
	  							if(this.adminUser){
	  								this.adminUser.set(obj.user);
	  							}
  								selectNode.set(obj.admin);
  								this.adminUser = selectNode;
	  						}
	  					});
  				},
  				scope: this
  			}]
  	});
      this.callParent();
  },
  listeners: {
	itemdblclick: function(me, r){
		var type = r.data.type;
		var id = r.data.obId;
		if (type === CDR.Constant.PBX_TYPE) {
			/*this.pbxForm.getForm().reset();
			Ext.Ajax.request({
				scope: this,
				async: false,
				url: 'pbx-info?pbxId=' + id,
				success: function(response, opts) {
					var obj = Ext.decode(response.responseText);
					this.pbxForm.editData(obj);
				}
			});
			this.userDetail.removeAll(false);
			this.userDetail.add(this.pbxForm);*/
		} else if (type === CDR.Constant.USER_TYPE) {
			//get data from server
			/*this.userForm.getForm().reset();
			Ext.Ajax.request({
				scope: this,
				async: false,
				url: 'user-info?userId=' + id,
				success: function(response, opts) {
					var obj = Ext.decode(response.responseText);
					this.userForm.eidtData(obj);
				}
			});
			this.userDetail.removeAll(false);
			this.userDetail.add(this.userForm);*/
		}
	},
	itemcontextmenu:function(el, r, node, index, e, eOpts){
		var type = r.data.type;
		 if (type === CDR.Constant.USER_TYPE) {
		 	this.select(r);
			this.menu.showAt(e.xy);
		 }
	}
  }
});